package com.lefevre.cms;

import com.lefevre.cms.utils.YmlUtils;
import com.lefevre.cms.web.action.install.InstallManage;
import com.lefevre.cms.web.filter.InitApplicationListener;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.SessionCookieConfig;
import javax.servlet.SessionTrackingMode;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;

/**
 * 拾柒
 *
 * @author Lefevre
 */
@EnableScheduling
@ServletComponentScan("com.lefevre.cms.web.filter")
@SpringBootApplication(exclude = {FreeMarkerAutoConfiguration.class})
public class LefevreCmsApplication extends SpringBootServletInitializer {

    // public static void main(String[] args) {
    //     SpringApplication.run(LefevreCmsApplication.class, args);
    // }


    public static void main(String[] args) {
        boolean isMaven = false;//是否由maven插件启动
        if (args != null && args.length > 0) {
            for (String arg : args) {
                if ("maven".equals(arg)) {
                    isMaven = true;
                    break;
                }
            }
        }
        if (!isMaven) {
            SpringApplication application = new SpringApplication(LefevreCmsApplication.class);
            //注册监听器
            application.addListeners(new InitApplicationListener());
            application.run(args);
        } else {//maven命令执行
            String user_dir = System.getProperty("user.dir");

            //默认外部目录
            String defaultExternalDirectory;

            //论坛外部目录
            Object externalDirectory = YmlUtils.getYmlProperty("application.yml", "bbs.externalDirectory");
            if (externalDirectory != null && !"".equals(externalDirectory.toString().trim())) {//如果已设置了论坛外部目录
                defaultExternalDirectory = externalDirectory.toString();
            } else {
                defaultExternalDirectory = user_dir + File.separator + "target" + File.separator + "bbs";
            }

            //生成外部文件夹
            InstallManage installManage = new InstallManage();
            List<String> folderList = installManage.folderList();
            if (folderList != null && folderList.size() > 0) {
                for (String folderPath : folderList) {
                    //生成文件目录
                    Path path = Paths.get(defaultExternalDirectory + File.separator + StringUtils.replace(folderPath, "/", File.separator));
                    if (!Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {//目录不存在
                        try {
                            Files.createDirectories(path);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    /**
     * 解决静态url带jsessionid问题
     */
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        servletContext.setSessionTrackingModes(Collections.singleton(SessionTrackingMode.COOKIE));
        SessionCookieConfig sessionCookieConfig = servletContext.getSessionCookieConfig();
        sessionCookieConfig.setHttpOnly(true);
    }

}
